﻿using System;
using System.Collections.Generic;
using System.Text;

using Obsidian.Edm;

namespace Obsidian.Web.Ui
{

    #region 参考样式

    /* 参考样式
     .pagingbar a {
        text-decoration: none;
        color:Jim;
    }

    .pagingbar a, .pagingbar span {
        padding:1px 5px 1px 5px;
        font-size:14px;
        margin-right:3px;
        border:solid 1px #B8B7B6;
        line-height:30px;
        font-size:12px;
    }

    .pagingbar .current-page { 
        border:solid 2px #547DBE; 
        font-weight:bold;
    }
     */

    #endregion

    /// <summary>
    /// 翻页条
    /// author:郑金泉
    /// updated:2010-04-21
    /// </summary>
    public class PagingBar
    {
        //总页数
        private int pagesCount = 1;
        //当前页码
        private int pageNo = 1;
        //显示按钮个数
        private int showNum = 9;
        //是否显示“上一页”和“下一页”按钮 
        private bool showPrevNextButtons = true;
        //是否显示跳跃按钮
        private bool showLeapButton = true;
        //页码传递参数名
        private string pageIndexParamName = null;

        /// <summary>
        /// 翻页条
        /// </summary>
        /// <param name="pageIndexParamName">页码参数名</param>
        /// <param name="listAttr">列表属性信息</param>
        public PagingBar(string pageIndexParamName, ListAttrInfo listAttr)
        {
            this.pageIndexParamName = pageIndexParamName;
            this.pageNo = listAttr.PageNo.Value;
            this.pagesCount = listAttr.PagesCount.Value;
        }

        /// <summary>
        /// 翻页条
        /// </summary>
        /// <param name="pageIndexParamName">页码参数名</param>
        /// <param name="pageNo">页码</param>
        /// <param name="pagesCount">总页数</param>
        public PagingBar(string pageIndexParamName, int pageNo, int pagesCount)
        {
            this.pageIndexParamName = pageIndexParamName;
            this.pageNo = pageNo;
            this.pagesCount = pagesCount;
        }

        /// <summary>
        /// 总页数
        /// </summary>
        public int PagesCount
        {
            get { return this.pagesCount; }
            set { this.pagesCount = value; }
        }

        /// <summary>
        /// 当前页码
        /// </summary>
        public int PageNo
        {
            get { return this.pageNo; }
            set { this.pageNo = value; }
        }

        /// <summary>
        /// 显示页码按钮个数
        /// </summary>
        public int ShowNum
        {
            get { return this.showNum; }
            set { this.showNum = value; }
        }

        /// <summary>
        /// 是否显示“上一页”和“下一页”按钮
        /// </summary>
        public bool ShowPrevNextButtons
        {
            get { return this.showPrevNextButtons; }
            set { this.showPrevNextButtons = value; }
        }

        /// <summary>
        /// 是否显示跳跃按钮
        /// </summary>
        public bool ShowLeapButton
        {
            get { return this.showLeapButton; }
            set { this.showLeapButton = value; }
        }

        /// <summary>
        /// 页码传递参数名
        /// </summary>
        public string PageIndexParamName
        {
            get { return this.pageIndexParamName; }
            set { this.pageIndexParamName = value; }
        }

        /// <summary>
        /// 生成html
        /// </summary>
        /// <param name="hrefFormat">翻页连接字符串格式</param>
        /// <param name="args">格式化参数</param>
        /// <returns></returns>
        public string Generate(string hrefFormat, params object[] args)
        {
            string PH_PAGE_ID = "%P%";

            if (this.pageNo < 1)
                throw new ArgumentException("当前页码不能小于1!");
            if (this.pageNo > this.pagesCount)
                throw new ArgumentException("当前页码不能大于总页数!");

            StringBuilder barBuilder = new StringBuilder();
            string and = args.Length == 0 ? "?" : "&";
            int start = this.pageNo - (this.showNum / 2);
            int end = 0;
            if (start < 2)
            {
                start = 2;
                if (this.showNum % 2 == 0 && this.pageNo % 2 == 0 && this.pageNo / 2 > this.showNum / 2)
                {
                    start++;
                    end = start + this.showNum - 1;
                }
                else
                {
                    end = this.showNum;
                }
            }
            else
            {
                if (this.showNum % 2 == 0)
                    start++;
                end = start + this.showNum - 1;
            }

            if (end >= this.pagesCount)
            {
                end = this.pagesCount - 1;
                start = end - this.showNum + 2;
                if (start < 2) start = 2;
            }

            int leap = this.pageNo + this.showNum;
            if (leap > this.pagesCount) leap = this.pagesCount;

            int leapback = this.pageNo - this.showNum;
            if (leapback < 1) leapback = 1;
            string formatStr = null;
            if (args != null)
                formatStr = String.Format(hrefFormat, args);
            else
                formatStr = hrefFormat;

            if (!String.IsNullOrEmpty(this.pageIndexParamName))
                formatStr += and + this.pageIndexParamName + "=" + PH_PAGE_ID;


            if (this.showPrevNextButtons)
            {
                if (this.pageNo == 1)
                    barBuilder.Append("<span>上一页</span>");
                else
                    barBuilder.Append("<a href=\"" + formatStr.Replace(PH_PAGE_ID, (this.pageNo - 1).ToString()) + "\">上一页</a>");
            }

            //第一页按钮
            if (this.pageNo == 1)
                barBuilder.Append("<span class=\"current-page\">1</span>");
            else
                barBuilder.Append("<a href=\"" + formatStr.Replace(PH_PAGE_ID, "1") + "\">1</a>");

            if (this.showLeapButton && start > 2)
                barBuilder.Append("<a href=\"" + formatStr.Replace(PH_PAGE_ID, leapback.ToString()) + "\">...</a>");

            for (int n = start; n <= end; n++)
            {
                if (n == this.pageNo)
                    barBuilder.Append("<span class=\"current-page\">" + n.ToString() + "</span>");
                else
                    barBuilder.Append("<a href=\"" + formatStr.Replace(PH_PAGE_ID, n.ToString()) + "\">" + n.ToString() + "</a>");
            }

            if (this.showLeapButton && end < this.pagesCount - 1)
                barBuilder.Append("<a href=\"" + formatStr.Replace(PH_PAGE_ID, leap.ToString()) + "\">...</a>");

            //最后一页
            if (this.pagesCount > 1)
            {
                if (this.pageNo == this.pagesCount)
                    barBuilder.Append("<span class=\"current_page\">" + this.pagesCount.ToString() + "</span>");
                else
                    barBuilder.Append("<a href=\"" + formatStr.Replace(PH_PAGE_ID, this.pagesCount.ToString()) + "\">" + this.pagesCount.ToString() + "</a>");
            }

            if (this.showPrevNextButtons)
            {
                if (this.pageNo == this.pagesCount)
                    barBuilder.Append("<span>下一页</span>");
                else
                    barBuilder.Append("<a href=\"" + formatStr.Replace(PH_PAGE_ID, (this.pageNo + 1).ToString()) + "\">下一页</a>");
            }

            return barBuilder.ToString();
        }
    }
}
